#include "handlemplayer.h"


HandleMplayer::HandleMplayer(MPlayerWrapper* pPlayer, MdiAreaPlayer* displayPlayer, QObject *parent) :
    QObject(parent)
{

 /*
signals restant:
void nextAsked(QString videoType);
void prevAsked(QString videoType);
*/

    player=pPlayer;
    QObject::connect(player,SIGNAL(started(int,double)),this, SIGNAL(started()));
    QObject::connect(player,SIGNAL(failedToStart(QString)),this, SIGNAL(failedToLoad(QString)));
    QObject::connect(player,SIGNAL(failed(QString)),this, SIGNAL(failedToLoad(QString)));
    QObject::connect(player,SIGNAL(videoFinished(int)),this, SLOT(finishToLoad()));

    //rediriger le signal clavier
    QObject::connect(displayPlayer,SIGNAL(mPlayerKeyPress(int)),this,SLOT(displayKeyEvent(int)));

}


HandleMplayer::~HandleMplayer()
{

}

void HandleMplayer::displayKeyEvent(int keyCode){
    if(Qt::Key_Escape == keyCode){
        qDebug()<<"onHome, pas d propagation";
        return;
    }
    if(!typeLoading.isEmpty()){
         qDebug()<<"evt clavier type:"<< typeLoading;
        emit(keyEvent(keyCode,typeLoading));
    }else {
        qDebug()<<"evt clavier mais pas de lecture!!";
        emit(keyEvent(keyCode,lastLoadingType));
    }
}

QString HandleMplayer::loadingType(){
    return this->typeLoading;
}

QString HandleMplayer::loadingVideoId(){
    return this->vIdLoading;
}

void HandleMplayer::finishToLoad(){
    qDebug()<<"lecture finie avec list etat:" << listStatePlaying;
    switch(listStatePlaying){
        case 0 :
            qDebug()<<"fini no list";
            emit(allFinished(typeLoading));
            break;

        case 1 :
            qDebug()<<"fini regular list";
            emit(nextAsked(typeLoading));
            break;
        case 2 :
            qDebug()<<"fini first elt";
            emit(allFinished(typeLoading));
            break;
        case 3 :
            qDebug()<<"fini last alt";
            emit(allFinished(typeLoading));
            break;
        default :
            qDebug()<<"cas inconnu!!!";
            break;
    }
    lastLoadingType=QString(typeLoading);
    typeLoading="";
    listStatePlaying=0;
    vIdLoading="";
}



/*
  0 --> noList
  1 --> regular list elt
  2 --> first list elt
  3 --> last list elt
  */
void HandleMplayer::loadNowVideo(QString videoType, QString videoId, int listState){
    typeLoading=videoType;
    vIdLoading=videoId;
    player->playVideo(videoType,videoId);
    listStatePlaying=listState;
}






